package com.mmy.controller.train;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.train.TTrainBase;
import com.mmy.domain.train.TTrainOrder;
import com.mmy.domain.train.VTrainOrder;
import com.mmy.domain.train.VTrainPlan;
import com.mmy.service.train.impl.TrainBaseServiceImpl;
import com.mmy.service.train.impl.TrainOrderServiceImpl;
import com.mmy.service.train.impl.TrainPlanServiceImpl;

/**
 * 类的介绍：列车信息
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class TrainBaseController {

	@Autowired
	TrainBaseServiceImpl trainBaseServiceImpl;
	@Autowired
	TrainOrderServiceImpl trainOrderServiceImpl;
	@Autowired
	TrainPlanServiceImpl trainPlanServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明：后台列车信息管理 显示列车基本信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人hanxl
	 * @date 创建日期 2020.11.18 修改时间2021.03.16
	 */
	@RequestMapping("/backtrainbaseindex")
	public String backTrainBaseIndex(Model model) {
//不论谁登录查询所有
		List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
		model.addAttribute("lstTrainBase", lstTrainBase);
		return "webbackground/train/TrainBaseIndex";
	}

	/**
	 * 函数功能说明： 异步--航班信息列表数据
	 * 
	 * @param 参数说明 vPlaneBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping("/backtrainbasejson")
	@ResponseBody
	public String backTrainBaseJson(HttpServletRequest request, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strTrainName = request.getParameter("trianName");

		// 表格数据
		List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findByTrainName(strTrainName, startIndex, count);

		model.addAttribute("lstTrainBase", lstTrainBase);
		// 数据总数量
		int total = trainBaseServiceImpl.queryBaseCount(strTrainName);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstTrainBase);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	/**
	 * 函数功能说明：进入创建列车页面
	 * 
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping(value = "/backcreatetrainbase")
	public String backToCreateTrainBase(Model model) {

		return "webbackground/train/CreateTrainBase";
	}

	/**
	 * 函数功能说明： 新建列车信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */

	@RequestMapping(value = "/backsavecreatetrainbase")
	public String CreateTrainBase(TTrainBase tTrainBase, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		trainBaseServiceImpl.createTrainBase(tTrainBase, sessionSysUser);
		return "redirect:backtrainbaseindex";

	}

	/**
	 * 函数功能说明： 修改列车信息进入 页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */

	@RequestMapping(value = "/backtoupdatetrainbase")
	public String backToUpdateTrainBase(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));

		TTrainBase dbTrainBase = trainBaseServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("dbTrainBase", dbTrainBase);

		return "webbackground/train/UpdateTrainBase";

	}

	/**
	 * 函数功能说明： 修改列车信息提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	@RequestMapping(value = "/backsaveupdatetrainbase")
	public String backSaveUpdateTrainBase(TTrainBase tTrainBase, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		trainBaseServiceImpl.updateTrainBase(tTrainBase, sessionSysUser);
		return "redirect:backtrainbaseindex";

	}

	@RequestMapping(value = "/trainbaseinfo")
	public String ToTrainBaseInfo(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);
		TTrainBase tTrainBase = trainBaseServiceImpl.findByID(iTrainID);
		model.addAttribute("tTrainBase", tTrainBase);

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		return "webbackground/train/trainbaseinfo";
	}

	@RequestMapping(value = "/ordertraininfo")
	public String ToOrderTrainInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);

		String strOrderID = tTrainOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VTrainPlan vTrainPlan = trainPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("vTrainPlan", vTrainPlan);

		return "webbackground/train/ordertraininfo";
	}

//收款打印页面
	@RequestMapping(value = "/printtrain")
	public String ToPrintTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);

		return "webbackground/train/printtrain";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcanceltrain")
	public String ToPrintCancelTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);

		return "webbackground/train/printcanceltrain";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashiertrain")
	public String ToCashierTrain(Model model) {
		try {

			List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
			model.addAttribute("slist", lstTrainBase);

			return "OA/cashier/cashiertrain";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashiertrainorder")
	public String ToCashierTrainOrder(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		model.addAttribute("iTrainID", iTrainID);

		return "OA/cashier/cashiertrainorder";
	}

	@RequestMapping(value = "/entercheckcashiertrain")
	public String EnterCheckCashierTrain(HttpServletRequest request, HttpSession session, String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<TTrainOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(i);

			if (tTrainOrder.getCashierState() == 0) {
				tTrainOrder.setCashierState((byte) 1);
			} else if (tTrainOrder.getCashierState() == 1) {
				tTrainOrder.setCashierState((byte) 2);
			} else {
				tTrainOrder.setCashierState((byte) 1);
			}
			tTrainOrder.setCashierDate(new Date());
			tTrainOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tTrainOrder);
		}

		trainOrderServiceImpl.saveAll(lstCashier);

		return "OA/cashier/cashiertrainorder";

	}

//查询
	@RequestMapping(value = "/searchcashiertrain")
	public String FindCashierTrainOrder(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
			model.addAttribute("orderlist", lstTrainOrder);
			model.addAttribute("iTrainID", iTrainID);
			return "OA/cashier/cashiertrainorder";
		} else {
			List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByCashierAndTrainID(strCashierState, iTrainID);
			model.addAttribute("orderlist", lstTrainOrder);
			model.addAttribute("iTrainID", iTrainID);
			return "OA/cashier/cashiertrainorder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accounttrain")
	public String ToAccountTrain(HttpServletRequest request, Model model) {

		List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
		model.addAttribute("slist", lstTrainBase);

		return "OA/account/accounttrain";
	}

	@RequestMapping(value = "/accounttrainorder")
	public String ToAccountTrainOrder(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		model.addAttribute("iTrainID", iTrainID);

		return "OA/account/accounttrainorder";
	}

	@RequestMapping(value = "/entercheckaccounttrain")
	public String EnterCheckAccountTrain(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<TTrainOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(i);

			if (tTrainOrder.getAccountState() == 0) {
				tTrainOrder.setAccountState((byte) 1);
			} else if (tTrainOrder.getAccountState() == 1) {
				tTrainOrder.setAccountState((byte) 2);
			} else {
				tTrainOrder.setAccountState((byte) 1);
			}
			tTrainOrder.setAccountDate(new Date());
			tTrainOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tTrainOrder);
		}

		trainOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accounttrainorder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittancetrain")
	public String EnterCheckRemittanceTrain(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<TTrainOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(i);

			if (tTrainOrder.getRemittanceState() == 0) {
				tTrainOrder.setRemittanceState((byte) 1);
			} else {
				tTrainOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tTrainOrder);
		}

		trainOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accounttrainorder";

	}

//查询
	@RequestMapping(value = "/searchaccounttrain")
	public String FindAccountTrain(HttpServletRequest request, Model model) {

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
			model.addAttribute("orderlist", lstTrainOrder);
			model.addAttribute("iTrainID", iTrainID);

			return "OA/account/accounttrainorder";
		} else {
			List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByAccountAndTrainID(strAccountState, iTrainID);
			model.addAttribute("orderlist", lstTrainOrder);
			model.addAttribute("iTrainID", iTrainID);

			return "OA/account/accounttrainorder";
		}

	}

	/**
	 * 函数功能说明： 删除列车信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/deltrainbase")
	@ResponseBody
	public String DelTrainBase(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstTrainID = new ArrayList<>();

		for (String str : checklist) {
			lstTrainID.add(Integer.parseInt(str));
		}

		trainBaseServiceImpl.delTrainBase(lstTrainID);

		return "webbackground/train/trainbase";
	}

	/**
	 * 函数功能说明： 查询列车信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchtrainbase")
	public String FindTrainBase(HttpServletRequest request, Model model) {

		String strTrainName = request.getParameter("TrainName");
		if (strTrainName == "") {
			List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
			model.addAttribute("trainbaselist", lstTrainBase);
			request.setAttribute("strTrainName", strTrainName);
			return "webbackground/train/trainbase";
		} else {
			List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findByName(strTrainName);
			model.addAttribute("trainbaselist", lstTrainBase);
			request.setAttribute("strTrainName", strTrainName);
			return "webbackground/train/trainbase";
		}

	}
}
